package com.qk.controller;


import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.CustomerDto;
import com.qk.entity.Customer;
import com.qk.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/customers")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 查询客户列表
     */
    @GetMapping
    public Result listCustomers(CustomerDto customerDto){
        log.info("数据请求成功 " + customerDto);
        PageResult<Customer> pageResult = customerService.listCustomers(customerDto);
        return Result.success(pageResult);
    }

    /**
     * 新增客户
     */
    @PostMapping
    public Result add(@RequestBody Customer customer){
        log.info("新增用户数据: {}", customer);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerService.save(customer);
        return Result.success();
    }

    /**
     * 根据Id查询客户
     */
    @GetMapping("/{id}")
    public Result getCustomerById(@PathVariable Integer id){
        log.info("查询id:{}", id);
        Customer customer = customerService.getById(id);
        return Result.success(customer);
    }
    /**
     * 修改客户
     */
    @PutMapping
    public Result updateCustomer(@RequestBody Customer customer){
        log.info("修改用户数据: {}", customer);
        customer.setUpdateTime(LocalDateTime.now());
        customerService.updateById(customer);
        return Result.success();
    }

}
